package org.tarena.dang.action.user;

import org.tarena.dang.action.BaseAction;

import org.tarena.dang.entity.User;
import org.tarena.dang.service.UserService;
import org.tarena.dang.service.UserServiceImp;
import org.tarena.dang.util.EncryptUtil;
import org.tarena.dang.util.VerifyCodeUtils;

public class CheckCodeAction extends BaseAction {
	private String code;
	private User user;
	
	public String execute() throws Exception {
		user = new User();
		session.put("checkCodeError", "邮箱验证码不正确！");
		UserService userService = new UserServiceImp();
		int id = Integer.parseInt(VerifyCodeUtils.getUserID(code));
		user.setId(id);
		user = userService.checkEmailCode(user);
		System.out.println(user.getNickname());
		String code = EncryptUtil.Base64Encrypt((user.getEmailVerifyCode()+"-"+id).getBytes());
		if(this.code.equals(code)){
			userService.updateEmailVerify(id);
			user.setLastLoginTime(System.currentTimeMillis());
			userService.update(user);
			session.put("d_user", user);
			System.out.println("邮箱验证码 OK！");
			System.out.println(user.getNickname());
			session.put("nickname", user.getNickname());
			return "success";
		}
		return "error";
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	
	
	
}
